<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="制版编码" prop="platemakingCode">
        <el-input
          v-model.trim="queryParams.platemakingCode"
          style="width: 200px"
          placeholder="请输入制版编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新/旧/补版" prop="isNew">
        <el-input
          v-model.trim="queryParams.isNew"
          style="width: 200px"
          placeholder="请输入新/旧/补版"
          @keyup.enter="handleQuery"
        />
        <!-- <el-select
                    v-model="queryParams.isNew"
                    clearable
                    style="width: 180px"
                    placeholder="请选择"
                    >
                    <el-option
                        v-for="item in ems_produce_is_new"
                        :key="item.label"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select> -->
      </el-form-item>
      <el-form-item label="车间" prop="workshopName">
        <el-select
          v-model="queryParams.workshopName"
          clearable
          style="width: 200px"
          placeholder="请选择"
        >
          <el-option
            v-for="item in workshopOptions"
            :key="item.name"
            :label="item.name"
            :value="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产单编码" prop="produceNo">
        <el-input
          v-model.trim="queryParams.produceNo"
          style="width: 200px"
          placeholder="请输入生产单编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版编码" prop="pubNo">
        <el-input
          v-model.trim="queryParams.pubNo"
          style="width: 200px"
          placeholder="请输入出版编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="制版人" prop="platemakingBy">
        <el-input
          v-model.trim="queryParams.platemakingBy"
          style="width: 200px"
          placeholder="请输入制版人"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制版日期" prop="platemakingDate">
        <el-date-picker
          style="width: 200px"
          v-model.trim="queryParams.platemakingDate"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="请选择日期"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>
    <div>
      <el-table v-loading="loading" border :data="dataList">
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="状态"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="status"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <dict-tag :options="ems_order_status" :value="row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="制版编码"
          min-width="195"
          align="center"
          :show-overflow-tooltip="true"
          prop="platemakingCode"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleView(row)" text>{{
              row.platemakingCode
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="新/旧版"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="isNew"
          v-if="columns[1].visible"
        >
          <!-- <template v-slot="{ row }">
                        <div>
                            <span v-for="(item, index) in row.isNew?.split(',')" :key="index">
                                <dict-tag :options="ems_produce_is_new" :value="item" />
                            </span>
                        </div>
                        </template> -->
        </el-table-column>
        <el-table-column
          label="车间"
          min-width="150"
          align="center"
          :show-overflow-tooltip="true"
          prop="workshopName"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <div>
              <span
                v-for="(item, index) in row.workshopName?.split(',')"
                :key="index"
              >
                {{ item }}
                <br />
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="生产单编码"
          min-width="180"
          align="center"
          :show-overflow-tooltip="true"
          prop="produceNo"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <div>
              <span
                v-for="(item, index) in row.produceNo?.split(',')"
                :key="index"
              >
                <el-button
                  type="primary"
                  @click="handleViewProduce(row, index)"
                  text
                  >{{ item }}</el-button
                >
                <br />
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="出版编码"
          min-width="150"
          align="center"
          :show-overflow-tooltip="true"
          prop="pubNo"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <div>
              <span v-for="(item, index) in row.pubNo?.split(',')" :key="index">
                {{ item }}
                <br />
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="书刊名称"
          min-width="150"
          align="center"
          :show-overflow-tooltip="true"
          prop="bookName"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <div>
              <span
                v-for="(item, index) in row.bookName?.split(',')"
                :key="index"
              >
                {{ item }}
                <br />
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="制版日期"
          align="center"
          min-width="150"
          :show-overflow-tooltip="true"
          prop="platemakingDate"
          v-if="columns[2].visible"
        >
        </el-table-column>
        <el-table-column
          label="制版人"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="platemakingBy"
          v-if="columns[3].visible"
        >
        </el-table-column>
        <el-table-column
          label="烤版人"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="bakedBy"
          v-if="columns[4].visible"
        >
        </el-table-column>
        <el-table-column
          label="备注"
          min-width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="remark"
          v-if="columns[5].visible"
        >
        </el-table-column>
        <el-table-column
          label="创建人"
          min-width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="createBy"
          v-if="columns[6].visible"
        >
        </el-table-column>
        <el-table-column
          label="创建时间"
          min-width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="createTime"
          v-if="columns[7].visible"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="120"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tooltip
              content="修改"
              placement="top"
              v-if="scope.row.status == 1"
            >
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              content="删除"
              placement="top"
              v-if="scope.row.status == 1"
            >
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
              ></el-button>
            </el-tooltip>
            <!-- <el-button link type="primary" v-if="scope.row.status == 1"
                            @click="handleSubmit(scope.row)">提交</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup name="Platemaking">
import { downLoadBlob } from "@/utils/ruoyi";
import {
  getPlatemakingList,
  delPlatemakingInfo,
  editPlatemaking,
  exportPlatemakingList,
} from "@/api/production/platemaking.js";
import { getMesWorkShop } from "@/api/basicInfo/workshop.js";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const total = ref(0);
const router = useRouter();
const workshopOptions = ref([]);
// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `制版编码`, visible: true },
  { key: 2, label: `制版日期`, visible: true },
  { key: 3, label: `制版人`, visible: true },
  { key: 4, label: `烤版人`, visible: true },
  { key: 5, label: `备注`, visible: true },
  { key: 6, label: `创建人`, visible: true },
  { key: 7, label: `创建时间`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    allotNum: undefined,
    allotUnitid: undefined,
    allotDate: undefined,
  },
});
const { queryParams } = toRefs(data);
const { ems_order_status, ems_produce_is_new } = proxy.useDict(
  "ems_order_status",
  "ems_produce_is_new"
);

const getList = () => {
  loading.value = true;
  getPlatemakingList(queryParams.value)
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

// 查看
const handleViewProduce = (row, index) => {
  const produceId = row.produceId?.split(",")[index];
  const produceNo = row.produceNo?.split(",")[index];
  if (produceNo.indexOf("FM") > -1) {
    router.push({
      path: "/production/cover/coverDetails",
      query: {
        id: produceId,
        info: true,
        moduleCode: "platemaking",
      },
    });
  } else {
    router.push({
      path: "/production/text/textDetails",
      query: {
        id: produceId,
        info: true,
        moduleCode: "platemaking",
      },
    });
  }
};

// 新增
const handleAdd = () => {
  router.push("/production/platemaking/platemakingDetails");
};
// 编辑
const handleUpdate = (row) => {
  router.push({
    path: "/production/platemaking/platemakingDetails",
    query: {
      id: row.id,
    },
  });
};
// 查看
const handleView = (row) => {
  router.push({
    path: "/production/platemaking/platemakingDetails",
    query: {
      id: row.id,
      info: true,
    },
  });
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除"' + row.platemakingCode + '"的做版单？')
    .then(function () {
      return delPlatemakingInfo(row.id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
// 提交
const handleSubmit = (row) => {
  proxy.$modal
    .confirm('是否确认提交"' + row.platemakingCode + '"的做版单？')
    .then(function () {
      return editPlatemaking({ status: 2, id: row.id });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("提交成功");
    })
    .catch(() => {});
};
// 导出
const handleExport = () => {
  exportPlatemakingList(queryParams.value).then((res) => {
    downLoadBlob(res, "制版列表");
  });
};

const getWorkShopList = async () => {
  const res = await getMesWorkShop({ pageNum: 1, pageSize: 10 });
  workshopOptions.value = [...res.rows];
};
getWorkShopList();

getList();
</script>

<style lang="scss" scoped></style>